package com.amiu.shiro;

import java.util.Collection;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.cache.Cache.ValueWrapper;

@SuppressWarnings("unchecked")
public class ShiroSpringCache<K, V> implements org.apache.shiro.cache.Cache<K, V>{
	
	private org.springframework.cache.Cache springCache;
	
	public ShiroSpringCache(org.springframework.cache.Cache cache){
		this.springCache = cache;
	}
	
	@Override
	public V get(K key) throws CacheException {
		ValueWrapper valueWrapper = springCache.get(key);
		if (valueWrapper == null) {
			return null;
		}
		return (V) valueWrapper.get();
	}

	@Override
	public V put(K key, V value) throws CacheException {
		springCache.put(key, value);
		return value;
	}

	@Override
	public V remove(K key) throws CacheException {
		V previous = get(key);
		springCache.evict(key);
		return previous;
	}

	@Override
	public void clear() throws CacheException {
		springCache.clear();
	}
	/**
	 * org.springframework.cache.Cache不支持实现
	 * org.apache.shiro.cache.Cache的
	 * size()，keys()，values()方法
	 * 
	 */
	@Override
	public int size() {
		throw new UnsupportedOperationException(
				"invoke spring cache abstract size method not supported");
	}
	@Override
	public Set<K> keys() {
		throw new UnsupportedOperationException(
				"invoke spring cache abstract keys method not supported");
	}
	@Override
	public Collection<V> values() {
		 throw new UnsupportedOperationException(
				 "invoke spring cache abstract values method not supported");
	}

}
